//
//  NewSongPublishViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NewSongPublishViewController.h"
#import "AFNetworking.h"
#import "AFNetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "NewsongCollectionViewCell.h"
#import "songleiViewController.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import"PlayListViewController.h"
#import "MBProgressHUD.h"
@interface NewSongPublishViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSInteger number;
}
@property(nonatomic,retain)NSMutableArray *modelArr;

@property (nonatomic,retain)UICollectionView *myCollectionView;
@property(nonatomic,retain)MBProgressHUD*HUD;
@end
@implementation NewSongPublishViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新歌首发";
        number = 1;
        self.modelArr = [NSMutableArray array];
    }

    return self;
}

- (void)data{
    NSString *str1 = @"http://online.dongting.com/recomm/new_songs_more?page=";
    NSString *str2 = [NSString stringWithFormat:@"%ld&size=30", number];
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
    [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
        
        NSMutableArray *arr = [result objectForKey:@"data"];
        for (NSInteger i=0; i<arr.count; i++) {
            MusicModel *movie = [[MusicModel alloc] init];
            NSMutableDictionary *movieDic = arr[i];
            [movie setValuesForKeysWithDictionary:movieDic];
            [self.modelArr addObject:movie];
            [movie release];
            
        }
        [self.myCollectionView reloadData];
        [_HUD hide:YES];
    }];
    
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    [self data];

    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 150 / 667);
    
    flowlayout.minimumLineSpacing = 40;
    
    flowlayout.minimumInteritemSpacing = 10;
    
    flowlayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 100 / 667);
    
    flowlayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667, self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowlayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    
    [self.myCollectionView registerClass:[NewsongCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self addFooter];
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.modelArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    
    NewsongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    MusicModel *music = self.modelArr[indexPath.row];
    cell.myLabel.text = music.title;
    NSURL *url = [NSURL URLWithString:music.pic];
    [cell.myImageView sd_setImageWithURL:url];
    return cell;
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    songleiViewController *song = [[songleiViewController alloc] init];
    song.musicModel = self.modelArr[indexPath.row] ;
    [self.navigationController pushViewController:song animated:YES];
    [song release];
    
    
    
}

-(void)addHeader {
    [self.myCollectionView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.myCollectionView reloadData];
            
            [self.myCollectionView headerEndRefreshing];
        });
    }];
    [self.myCollectionView headerBeginRefreshing];
}

-(void)addFooter {
    [self.myCollectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self data];
            [self.myCollectionView reloadData];
            
            [self.myCollectionView footerEndRefreshing];
        });
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
